import { ConfigType, registerAs } from '@nestjs/config';
import { env, envBoolean, envNumber } from '~/utils/env';
import { DataSourceOptions } from 'typeorm';
export const dbRegToken = 'database';

const dataSourceOptions: DataSourceOptions = {
  type: 'mysql',
  host: env('DB_HOST', '8.138.168.54'),
  port: envNumber('DB_PORT', 3306),
  username: env('DB_USERNAME', 'root'),
  password: env('DB_PASSWORD', 'huang'),
  database: env('DB_DATABASE', 'nest_admin'),
  synchronize: envBoolean('DB_SYNCHRONIZE', false),
  // 解决通过 pnpm migration:run 初始化数据时，遇到的 SET FOREIGN_KEY_CHECKS = 0; 等语句报错问题, 仅在执行数据迁移操作时设为 true
  multipleStatements: false,
  entities: ['dist/modules/**/*.entity{.ts,.js}'],
  migrations: ['dist/migrations/*{.ts,.js}'],
  subscribers: ['dist/modules/**/*.subscriber{.ts,.js}']
};
export const DatabaseConfig = registerAs(dbRegToken, (): DataSourceOptions => dataSourceOptions);
export type IDatabaseConfig = ConfigType<typeof DatabaseConfig>;
// const dataSource = new DataSource(dataSourceOptions);
// export { dataSource };
